Contents
  1. 1. 解密old version
  2. 2. 解密new version
  3. 3. 恶意更新固件
  4. 4. IOT Auditor
  5. 5. 后续

固件下载地址:http://files.dlink.com.au/Products/DIR-850L/REV_B/Firmware/Firmware_v2.20b03/DIR850LB1_FW220WWb03.bin

1
2
3
4
$ binwalk DIR850LB1_FW220WWb03.bin 

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------

binwalk并没有分析出什么东西,这可能是一个加密文件。我们需要用到binwalk的-E参数,查看该固件的。计算熵是一种感知给定字节序列是否压缩或加密的有效手段。熵值大,意味着有可能是加密的或是压缩过的。熵值低,则正好相反。不过,即使得到具体的熵值,也并不是总能立刻得到准确的判断。

解密old version

https://pierrekim.github.io/blog/2017-09-08-dlink-850l-mydlink-cloud-0days-vulnerabilities.html中可以学到:

DIR850LA1_FW114WWb07.bin是没有被加密的,直接解压后,后面版本的加密key wrgac05_dlob.hans_dir850l 就存在于固件系统目录下的/etc/configfw_signimage_sign中,一般来说同一型号不同版本的路由器的key都是一样的。利用文中的解密方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* 
* Simple tool to decrypt D-LINK DIR-850L REVB firmwares
*
* $ gcc -o revbdec revbdec.c
* $ ./revbdec DIR850L_REVB_FW207WWb05_h1ke_beta1.bin wrgac25_dlink.2013gui_dir850l > DIR850L_REVB_FW207WWb05_h1ke_beta1.decrypted
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define USAGE "Usage: decimg <filename> <key>\n"

int main(int argc,
char **argv)
{
int i, fi;
int fo = STDOUT_FILENO, fe = STDERR_FILENO;

if (argc != 3)
{
write(fe, USAGE, strlen(USAGE));
return (EXIT_FAILURE);
}

if ((fi = open(argv[1], O_RDONLY)) == -1)
{
perror("open");
write(fe, USAGE, strlen(USAGE));
return (EXIT_FAILURE);
}

const char *key = argv[2];
int kl = strlen(key);

i = 0;
while (1)
{
char buffer[4096];
int j, len;
len = read(fi, buffer, 4096);
if (len <= 0)
break;
for (j = 0; j < len; j++) {
buffer[j] ^= (i + j) % 0xFB + 1;
buffer[j] ^= key[(i + j) % kl];
}
write(fo, buffer, len);
i += len;
}

return (EXIT_SUCCESS);
}

编译后进行尝试解密原文测试的固件

1
2
3
4
5
$ ./revbdec DIR850LB1_FW207WWb05.bin wrgac05_dlob.hans_dir850l > DIR850LB1_FW207WWb05.decrypted
$ binwalk DIR850LB1_FW207WWb05.decrypted

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------

失败,这是为什么?因为我找到的key和原文中的keywrgac25_dlink.2013gui_dir850l并不相同,看来同一个型号的固件key也会不同

1
2
3
4
5
6
7
8
9
$ ./revbdec DIR850LB1_FW207WWb05.bin wrgac25_dlink.2013gui_dir850l > DIR850LB1_FW207WWb05.decrypted
$ binwalk DIR850LB1_FW207WWb05.decrypted

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
0 0x0 DLOB firmware header, boot partition: "dev=/dev/mtdblock/1"
10380 0x288C LZMA compressed data, properties: 0x5D, dictionary size: 8388608 bytes, uncompressed size: 5184868 bytes
1704052 0x1A0074 PackImg section delimiter tag, little endian size: 10517760 bytes; big endian size: 8232960 bytes
1704084 0x1A0094 Squashfs filesystem, little endian, version 4.0, compression:lzma, size: 8231815 bytes, 2677 inodes, blocksize: 131072 bytes, created: 2016-03-29 04:08:14

对DIR850LB1_FW207WWb05.bin测试成功,再测试FW220

1
2
3
4
5
6
# 两个key我都已经尝试
$ ./revbdec DIR850LB1_FW220WWb03.bin wrgac05_dlob.hans_dir850l > DIR850LB1_FW220WWb03.decrypted
$ binwalk DIR850LB1_FW220WWb03.decrypted

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------

失败,可能是由于固件的加密方式已改变

固件一般有这么几种加密方式:

  1. 设备固件出厂时未加密,也不包含任何解密例程。解密例程与固件的未加密版本一起以更高版本(v1.1)一起提供,以用于将来的加密固件更新,随后的固件版本将被加密。

1

  1. 设备固件在原始发行版中已加密,供应商决定更改加密方案,并发布包含新解密例程的未加密过渡版本v1.2。阅读固件版本的发行说明可能有助于识别未加密的过渡版本。发行说明通常会指导用户在升级到最新版本之前先升级到中间版本,中间版本很可能是未加密的过渡固件。

  1. 设备固件在原始版本中是加密的。但是,供应商决定更改加密方案,并发布包含新解密例程的未加密过渡版本。在这种情况下,来获得解密例程就很困难了。一种方法是购买设备并直接从硬件中提取未加密的固件。另一种可能的方法是对固件进行更多的分析,以期“破解加密”。

解密new version

通过分析DIR850L B1系列的路由器,发现DIR850LB1_FW210WWb03.bin没有被加密,所以应该就是上述的加密方式2,新的解密方式应该就保存在FW210中

1
2
3
4
5
6
7
8
9
$ binwalk DIR850LB1_FW210WWb03.bin 

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
0 0x0 DLOB firmware header, boot partition: "dev=/dev/mtdblock/1"
10380 0x288C LZMA compressed data, properties: 0x5D, dictionary size: 8388608 bytes, uncompressed size: 5213748 bytes
209586 0x332B2 Nagra Constant_KEY IDEA_Key: 10192431 B9CB4D0F BA90B26E
1704052 0x1A0074 PackImg section delimiter tag, little endian size: 13664256 bytes; big endian size: 8441856 bytes
1704084 0x1A0094 Squashfs filesystem, little endian, version 4.0, compression:lzma, size: 8441157 bytes, 2692 inodes, blocksize: 131072 bytes, created: 2017-09-18 12:11:33

在ida中查找加密关键字crypt发现了关键函数encrypt_main->sub_4090E0

crypt

还可以注意到,在encrypt_main调用的sub_408F8C中,似乎是对文件进行encode decode处理的函数

而在我们的固件系统文件中存在/usr/sbin/encimg,尝试运行一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ sudo chroot . ./qemu-mips-static usr/sbin/encimg
no signature specified!
Usage: encimg {OPTIONS}
-h : show this message.
-v : Verbose mode.
-i {input image file} : input image file.
-e : encode file.
-d : decode file.
-s : signature.
# input文件使用绝对路径相对路径都找不到文件,最后还是把文件贴到了squashfs-root根目录下
$ sudo chroot . ./qemu-mips-static --strace usr/sbin/encimg -i DIR850LB1_FW220WWb03.bin -d -s wrgac25_dlink.2013gui_dir850l
# 解密成功!
$ binwalk DIR850LB1_FW220WWb03.bin

DECIMAL HEXADECIMAL DESCRIPTION
--------------------------------------------------------------------------------
0 0x0 DLOB firmware header, boot partition: "dev=/dev/mtdblock/1"
10380 0x288C LZMA compressed data, properties: 0x5D, dictionary size: 8388608 bytes, uncompressed size: 5213748 bytes
209586 0x332B2 Nagra Constant_KEY IDEA_Key: 10192431 B9CB4D0F BA90B26E
1704052 0x1A0074 PackImg section delimiter tag, little endian size: 13664256 bytes; big endian size: 8441856 bytes
1704084 0x1A0094 Squashfs filesystem, little endian, version 4.0, compression:lzma, size: 8441027 bytes, 2692 inodes, blocksize: 131072 bytes, created: 2017-09-18 12:21:21

恶意更新固件

对固件恶意修改后进行打包再加密,升级后获取shell

IOT Auditor

  1. 熵图对比

  2. 分析Linux文件系统并下载所有内容

  3. 反编译的代码和函数

  4. https://expliot.io/pages/firmware-auditor

reference:

https://cq674350529.github.io/2019/03/18/D-Link-DIR-850L%E8%B7%AF%E7%94%B1%E5%99%A8%E5%88%86%E6%9E%90%E4%B9%8B%E8%8E%B7%E5%8F%96%E8%AE%BE%E5%A4%87shell/

https://mp.weixin.qq.com/s/CpT4gTAwYMCFqwTRxQBBkw

https://www.4hou.com/posts/wZpJ

后续

DIR-850L的最新固件DIR850LB1_FW223WWb01也是被加密的,而解密进程就在FW220中,使用上述方法即可解密固件^_^